package com.zlzlib.libindexpy.helper;

import android.text.TextUtils;

import com.github.promeg.pinyinhelper.Pinyin;
import com.zlzlib.libindexpy.bean.BasePyIndexBean;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by zlz on 2019/11/4.
 */
public class PyDataHelperImpl implements PyDataHelper {

    private int sortLength = 5;
    //无法读取的数据时排序的tag值  长度大于排序的长度 这样就会拍在最后
    private String xTag = "ZZZZZZ";

    @Override
    public void setSortLength(int length) {
        sortLength = length;
        StringBuilder tag = new StringBuilder();
        for (int i = 0; i <= length; i++) {
            tag.append("Z");
        }
        xTag = tag.toString();
    }

    @Override
    public PyDataHelper convert(List<? extends BasePyIndexBean> data) {
        if (null == data || data.isEmpty()) {
            return this;
        }
        int size = data.size();
        for (int i = 0; i < size; i++) {
            BasePyIndexBean bean = data.get(i);
//            StringBuilder pySb = new StringBuilder();
            // 是否需要转化
            if (bean.isNeedToPinyin()) {
                String target = bean.needToPinyinString();//取出需要被拼音化的字段
//                //遍历target的每个char得到它的全拼音
//                for (int i1 = 0; i1 < target.length(); i1++) {
//                    //利用TinyPinyin将char转成拼音
//                    //查看源码，方法内 如果char为汉字，则返回大写拼音
//                    //如果c不是汉字，则返回String.valueOf(c)
//                    pySb.append(Pinyin.toPinyin(target.charAt(i1)).toUpperCase());
//                }
                bean.setPinyin(Pinyin.toPinyin(target.toUpperCase(), ""));//设置 全拼音
            }
        }
        return this;
    }

    @Override
    public PyDataHelper fillIndexTag(List<? extends BasePyIndexBean> data, boolean isNumber) {
        if (null == data || data.isEmpty()) {
            return this;
        }
        int size = data.size();
        for (int i = 0; i < size; i++) {
            BasePyIndexBean bean = data.get(i);
            if (bean.isNeedToPinyin()) {
                String tagString = "";
                //以下代码设置拼音首字母
                if (!TextUtils.isEmpty(bean.getPinyin())) {
                    tagString = bean.getPinyin().substring(0, 1);
                }
                if (tagString.matches("[A-Z]")) {//如果是A-Z字母开头
                    bean.setBaseIndexTitle(tagString);
                    //判断前五位 来排序
                    bean.setBaseIndexTag(bean.getPinyin()
                            .substring(0, bean.getPinyin().length() > sortLength ? sortLength : bean.getPinyin().length()));
                } else if (isNumber && tagString.matches("[0-9]")) {
                    bean.setBaseIndexTitle(tagString);
                    //判断前五位 来排序
                    bean.setBaseIndexTag(bean.getPinyin()
                            .substring(0, bean.getPinyin().length() > sortLength ? sortLength : bean.getPinyin().length()));
                } else {
                    //特殊字母这里统一用#处理
                    bean.setBaseIndexTitle("#");
                    //把特殊的排到最后
                    bean.setBaseIndexTag(xTag);
                }
            }
        }
        return this;
    }

    @Override
    public PyDataHelper sortSourceData(List<? extends BasePyIndexBean> data, boolean isNumber) {
        if (null == data || data.isEmpty()) {
            return this;
        }
        convert(data);
        fillIndexTag(data, isNumber);
        //对数据源进行排序
        try {
            Collections.sort(data, new Comparator<BasePyIndexBean>() {
                @Override
                public int compare(BasePyIndexBean lhs, BasePyIndexBean rhs) {
                    return lhs.getBaseIndexTag().compareTo(rhs.getBaseIndexTag());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public PyDataHelper getSortedIndexData(List<? extends BasePyIndexBean> sourceData, List<String> data) {
        if (null == sourceData || sourceData.isEmpty()) {
            return this;
        }
        //按数据源来 sourceData 已经有序
        int size = sourceData.size();
        String title;
        for (int i = 0; i < size; i++) {
            title = sourceData.get(i).getTitle();
            if (!data.contains(title)) {//则判断是否已经将这个索引添加进去，若没有则添加
                data.add(title);
            }
        }
        return this;
    }
}
